var clickSubmit = false;//是否点击了保存
layui.use(['form','layer','laydate','table','laytpl','element'],function(){
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery;
    var currentIndex = parent.layer.getFrameIndex(window.name);//获取当前窗体索引

    //保存
    form.on('submit(save)', function(formData){
        //弹出loading
        var load = layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        var bo = verification(formData.form,null);
        if(bo){
            var submitData = formData.field;
            saveData(submitData,load);
        }else{
            layer.close(load);
        }
    });

    function saveData(submitData,load){
        try{
            if(clickSubmit == true){//防重复提交
                return;
            }
            clickSubmit = true;
            //保存公共流程节点
            $.ajax({
                type: "POST",
                url:  baseUrl+'/industryType/save',
                dataType: 'json',
                data: submitData,
                success: function (data) {
                    layer.close(load);
                    if(data.code>0){
                        layer.msg("保存成功");
                        //关闭本窗体
                        setTimeout(function () {
                            parent.reloadTable();
                            $("#cancel").click();
                        },1000);
                    }else{
                        layer.msg(data.msg);
                    }
                },
                error: function () {
                    layer.close(load);
                    layer.msg("保存失败!");
                    return ;
                }
            });
        }catch (e){
            layer.msg("操作异常");
        }finally {
            clickSubmit = false;
        }
    }

    $('#cancel').on('click',function () {
        currentIndex = parent.layer.getFrameIndex(window.name);//获取当前窗体索引
        parent.layer.close(currentIndex);//执行关闭
    });

    //验证表单
    function verification(form,iframeWin) {
        var config = {
            verify: {
                required: [
                    /[\S]+/
                    , '必填项不能为空'
                ]
            }
        };
        formElem = $(form);
        var button = $(this), verify = config.verify, stop = null
            , DANGER = 'layui-form-danger',PROMPT='required_prompt', field = {}
            , verifyElem = formElem.find('*[lay-verify-zdy]') //获取需要校验的元素
            , checkedElem = formElem.find('*[data-required]') //获取复选框
            ,rangeElem = formElem.find('*[data-range]')//获取范围判断的区间值

        //开始校验
        layui.each(verifyElem, function (_, item) {
            var othis = $(this), tips = '';
            var arr = othis.attr('lay-verify-zdy').split(',');
            for (var i in arr) {
                var ver = arr[i];
                var value = othis.val(), isFn = typeof verify[ver] === 'function';
                if(ver == 'number'&&value==''){
                    value = 0;
                }
                othis.removeClass(DANGER);
                if (verify[ver] && (isFn ? tips = verify[ver](value, item) : !verify[ver][0].test(value))) {
                    layer.msg(tips || verify[ver][1], {
                        icon: 5
                        , shift: 6
                    });
                    //非移动设备自动定位焦点
                    if (!layui.device().android && !layui.device().ios) {
                        if(item.type=='text'){
                            item.focus();
                        }else if(item.type=="select-one"){
                            $(item).next("div").find("input").focus();
                            $(item).next("div").find("input").attr("readonly");
                        }
                    }
                    othis.addClass(DANGER);
                    return stop = true;
                }
            }
        });
        if (stop) return false;
        //返回序列化表单元素， JSON 数据结构数据。
        return true;
    }
});



